﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;

namespace Common
{
    public static class HttpHelper
    {
        /// <summary>
        /// 向一个连接发送Post请求
        /// </summary>
        public static string Post(string url, string requestJson)
        {
            HttpContent httpContent = new StringContent(requestJson);
            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            HttpClient httpClient = new HttpClient();
            HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
            if (response.IsSuccessStatusCode)
            {
                string responseJson = response.Content.ReadAsStringAsync().Result;
                return responseJson;
            }
            else
            {
                Logger.Info("Error,StatusCode:" + response.StatusCode.ToString());
                throw new Exception("Error,StatusCode:" + response.StatusCode.ToString());
            }
        }

        /// <summary>
        /// 向一个连接发送Get请求
        /// </summary>
        public static string Get(string url)
        {
            HttpClient httpClient = new HttpClient();
            HttpResponseMessage response = httpClient.GetAsync(url).Result;
            if (response.IsSuccessStatusCode)
            {
                string responseJson = response.Content.ReadAsStringAsync().Result;
                return responseJson;
            }
            else
            {
                Logger.Info("Error,StatusCode:" + response.StatusCode.ToString());
                throw new Exception("Error,StatusCode:" + response.StatusCode.ToString());
            }
        }

        public static string PostWithHeaders(string url, string requestJson, KeyValuePair<string, string> header)
        {
            HttpContent httpContent = new StringContent(requestJson);
            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(header.Key, header.Value);
                HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
                if (response.IsSuccessStatusCode)
                {
                    string responseJson = response.Content.ReadAsStringAsync().Result;
                    return responseJson;
                }
                else
                {
                    Logger.Info("Error,StatusCode:" + response.StatusCode.ToString());
                    throw new Exception("Error,StatusCode:" + response.StatusCode.ToString());
                }
            }
        }

        public static Image GetUrlImage(string url)
        {
            try
            {
                HttpClient httpClient = new HttpClient();
                HttpResponseMessage response = httpClient.GetAsync(url).Result;
                if (response.IsSuccessStatusCode)
                {
                    Stream responseJson = response.Content.ReadAsStreamAsync().Result;
                    return Image.FromStream(responseJson);
                }
                else
                {
                    Logger.Info("Error,StatusCode:" + response.StatusCode.ToString());
                    return null;
                }
            }
            catch (Exception ex)
            {
                Logger.Info(ex.Message);
                return null;
            }
        }
    }
}
